<?php
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $ringId = !empty($match[2]) ? $match[2] : 0;        
        $refCode = !empty($match[4]) ? $match[4] : "";        
        
        
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["ringId"] = $ringId;        
        
        return $result;
    }
    
    function InsertSmsLogRing($data)
    {
        $sql = "INSERT INTO c_sms_log SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function UpdateDownload($id)
    {
        $sql = "UPDATE c_ring_back SET download=(download+1) WHERE id=" . $id;
        $result = @mysql_query($sql);            
        return $result;
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx = "/^TVDNC(\s+)(\d+)(\s+)([a-zA-Z0-9]{1,})$/si";                
        if(preg_match($regx, $data["content"])){
            
            $syntax = buildSyntax($data["content"], $regx);
            $list_service_code = array($data["service_code_db"]);                                    
                        
            $data["userId"] = $syntax["userId"];
            $data["id"] = $syntax["ringId"];            
            
            if(!empty($data["userId"]) && !empty($data["id"])){
                if(in_array($data["serviceCode"], $list_service_code)){
                    
                    $data["status"] = 1; 
                    
                    $response["status"] = 1;                        
                    $response["msg"] = linkDownload($syntax["ringId"]);
                    
                    /* Cập nhật lượt download */
                    UpdateDownload($data["id"]);
                      
                } else {
                    $response["status"] = 3;                        
                    $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                                                            
                    $data["status"] = 0;                 
                }   
            } else {
                $response["status"] = 2;                
                $response["msg"] = "Tin nhan sai cu phap";    
                                                                
                $data["status"] = 0;                
            }   
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
                        
            $data["userId"] = 0;                
            $data["status"] = 0;
            $data["id"] = 0;      
        }
        
        /* log sms */
        $data["transactionId"] = $data["transactionId"];
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 1; //ID partner HDC            
        $data["type"] = 4;
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];        
        
        $result = InsertSmsLogRing($data);   
        return $response;
    }
    
    function encodeHex($id)
    {    
        $str = dechex($id + 27111986);        
        $subStr = substr(dechex($id + 26101984), -5); //last three characters        
        $str = $str . $subStr;
        
        return $str;
    }
      
    function linkDownload($ringId)
    {
        return "http://cucdinh.mobi/tai-nhac-chuong-" . encodeHex($ringId);
    } 
?>
